from selenium.webdriver.firefox.webdriver import WebDriver

from locators import AuthorizationLocators
from pages.base import BasePage
from pages.sign_in.AuthorizationPage import AuthorizationPage


class AuthenticationPage(BasePage):
    URl_BY_BACK_BT = "https://passport.yandex.ru/auth/add"
    URL = "https://passport.yandex.ru/auth"

    def __init__(self, driver: WebDriver):
        super().__init__(driver)

    def input_login(self, login: str):
        if self.check_element(AuthorizationLocators.TOGGLE):
            self._switch_to_legacy_toggle()

        login_field = self.find_element(AuthorizationLocators.LOGIN)
        login_field.send_keys(login)
        submit_bt = self.find_element(AuthorizationLocators.SUBMIT)
        submit_bt.submit()

        self.wait_new_page(AuthorizationPage.URL)

        return AuthorizationPage(self.driver)

    def _switch_to_legacy_toggle(self):
        buttons = self.find_element(AuthorizationLocators.TOGGLE)
        buttons.click()

    def get_error_text(self):
        error_text = self.get_element_text(AuthorizationLocators.ERROR)
        return error_text
